#include "../include/head.h"
#include "../include/func.h"

int init_cmd(char *buf, pCmd_t pcmd) {
    char *token = NULL;
    const char s[] = {' ', '\n'}; // 将分隔符设为空格和换行符

    token = strtok(buf, s); // 首次调用strtok，传入待分割的字符串和分隔符集合
    // 循环提取所有token
    while (token != NULL) {
        strcpy(pcmd->_cmd[pcmd->_argc], token); // 将当前token复制到pcmd的_cmd数组中
        pcmd->_argc++; // _argc记录参数个数
        token = strtok(NULL, s); // 后续调用传入 NULL，表示继续从上次位置分割
    }
    return 0;
}

int cmd_check(pCmd_t pcmd) {
    char tmp[31] = {0}; // 用于存放待判断的命令
    strncpy(tmp, pcmd->_cmd[0], 31); 
    // strcmp函数返回0表示两个字符串一致
    if (!strcmp(tmp, "cd") ||
        !strcmp(tmp, "ls") ||
        !strcmp(tmp, "pwd") ||
        !strcmp(tmp, "rm") ||
        !strcmp(tmp, "puts") ||
        !strcmp(tmp, "gets") ||
        !strcmp(tmp, "mkdir") ||
        !strcmp(tmp, "rmdir")) {
        return 0;
    } else {
        return -1;
    }
}

int print_cmd(pCmd_t pcmd) {
    printf("cmd: "); 
    for (int i = 0; i < pcmd->_argc; ++i) { // pcmd->_argc为参数个数
        printf("%s ", pcmd->_cmd[i]); // pcmd->_cmd[i] 为一个参数
    }
    printf("\n");
    return 0;
}
